package com.inns.position.hbase;

import com.inns.position.utils.DateUtil;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by phoenix on 2018/4/22.
 */
public enum StatType {

    MINUTE(0) {
        @Override
        public long getEndTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            int h = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE) / 5 * 5;
            Date date = DateUtil.newDate(y, m + 1, d, h, minute);
            return date.getTime();
        }

        @Override
        public long getStartTime(long time) {
            return getEndTime(time) - 5 * HbaseConsts.ONE_MINUTE;
        }
    },
    /**
     * <code>HOUR = 1;</code>
     */
    HOUR(1) {
        @Override
        public long getEndTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            int h = calendar.get(Calendar.HOUR_OF_DAY);
            Date date = DateUtil.newDate(y, m + 1, d, h);
            return date.getTime();
        }

        @Override
        public long getStartTime(long time) {
            return getEndTime(time) - HbaseConsts.ONE_HOUR;
        }
    },
    /**
     * <code>DAY = 2;</code>
     */
    DAY(2) {
        @Override
        public long getEndTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            Date date = DateUtil.newDate(y, m + 1, d);
            return date.getTime();
        }

        @Override
        public long getStartTime(long time) {
            return getEndTime(time) - HbaseConsts.ONE_DAY;
        }
    },
    /**
     * <code>WEEK = 3;</code>
     */
    WEEK(3) {
        @Override
        public long getEndTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            LocalDate inputDate = LocalDate.of(y, m + 1, d);
            TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
//            System.out.println(inputDate.with(FIRST_OF_WEEK));
//            TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
//            System.out.println(inputDate.with(LAST_OF_WEEK));
            LocalDate ld = inputDate.with(FIRST_OF_WEEK);
            Date date = DateUtil.newDate(ld.getYear(), ld.getMonthValue(), ld.getDayOfMonth());
            return date.getTime();
        }

        @Override
        public long getStartTime(long time) {
            return getEndTime(time) - HbaseConsts.ONE_WEEK;
        }
    },
    /**
     * <code>MONTH = 4;</code>
     */
    MONTH(4) {
        @Override
        public long getEndTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);// 0~11 而 DateUtil 需要传入的是 1~12
            m++;
            Date date = DateUtil.newDate(y, m);
            return date.getTime();
        }

        @Override
        public long getStartTime(long time) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);// 0~11 而 DateUtil 需要传入的是 1~12
            if (m == 0) {
                y--;
                m = 12;
            }
            Date date = DateUtil.newDate(y, m);
            return date.getTime();
        }
    };

    private int type;

    private static final StatType[] VALUES = values();

    StatType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public static StatType valueOf(int type) {
        switch (type) {
            case 0:
                return MINUTE;
            case 1:
                return HOUR;
            case 2:
                return DAY;
            case 3:
                return WEEK;
            case 4:
                return MONTH;
            default:
                return DAY;
        }
    }

    public abstract long getEndTime(long time);

    public abstract long getStartTime(long time);
}
